var Pagination = new Class({
	Implements: Options,
	
	options:{
		el_next: null,
		el_prev: null,
		el_textField: null,
		style:{buttonClass:'button', fieldClass:'current'},
		onPageChange:$empty,
		onPageCountChange:$empty,
	},
	
	attachTo:null,
	pageNumber: 1,
	pageCount: 1,
	
	initialize: function(el, options){
		this.attachTo = el;
		this.setOptions(options).attach();
	},
	
	/**
	 * create pagination button
	 */
	attach: function(){
		//prevent double attachment
		if (this.attachTo.retrieve('pagination')) return;
		
		var opt = this.options;
		if (!$chk(opt.el_prev)){
			opt.el_prev = new Element('input', {type:'button', class:opt.style.buttonClass});
			opt.el_prev.set('value','Prev').inject(this.attachTo)
				.addEvent('click', function(){
					if (this.pageNumber-1>0){
						this.setPageNumber(this.pageNumber-1);
					}
				}.bind(this));
		}
		
		if (!$chk(opt.el_textField)){
			opt.el_textField = new Element('input',{type:'text', name:'', maxlength:'3', class:opt.style.fieldClass});
			opt.el_textField.inject(this.attachTo);
			opt.el_textField.value = this.pageNumber;
			opt.el_textField.addEvent('keyup',function(){
				if (event.keyCode == 13){
					opt.el_textField.value = opt.el_textField.value<0? 1 : opt.el_textField.value;
					opt.el_textField.value = opt.el_textField.value>this.pageCount? this.pageCount : opt.el_textField.value;
					this.setPageNumber(opt.el_textField.value);
				}
			}.bind(this));
		}
		
		opt.el_page = new Element('span');
		opt.el_page.set('html', ' / ' + this.pageCount).inject(this.attachTo);
		
		if (!$chk(opt.el_next)){
			opt.el_next = new Element('input',{type:'button', class:opt.style.buttonClass});
			opt.el_next.set('value','Next').inject(this.attachTo)
				.addEvent('click', function(){
					if (this.pageNumber+1<=this.pageCount){
						this.setPageNumber(this.pageNumber+1);
					}
				}.bind(this));
		} 
		
		this.attachTo.store('pagination', this);//prevent double attachment
	},
	
	setPageCount: function(count){
		this.pageCount = count;
		this.options.el_page.set('html', ' / ' + count);
		
		//karena jumlah halaman berubah, set index halaman ke 1
		this.pageNumber = 1;
		this.options.el_textField.value = 1;
		
		//fire change event
		this.firePageCountChange(count);
	},
	
	setPageNumber: function(index){
		this.pageNumber = index;
		this.options.el_textField.value = index;
		this.firePageChange(index);
	},
	
	getPageNumber: function(){
		return this.pageNumber;
	},
	
	firePageChange: function(index){
		if (this.options.onPageChange){
			this.options.onPageChange(this, index);
		}
	},
	
	firePageCountChange: function(count){
		//hanya satu halaman, so kita sembunyikan
		if (this.pageCount<=1){
			this.attachTo.fade('out');
		}
		else{
			this.attachTo.fade('in');
		}
		if (this.options.onPageCountChange){
			this.options.onPageCountChange(this, count);
		}
	},
	
	destroy: function(){
		this.options.el_curent.destroy();
		this.options.el_next.destroy();
		this.options.el_page.destroy();
		this.options.el_prev.destroy();
		this.attachTo.destroy();
		this.destroy();
	}
});